package com.yhp.mvc;

import org.apache.commons.collections.map.HashedMap;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class HandLerMapping {

    public static Map<String,MVCMapping> data = new HashMap<>();

    public static MVCMapping get(String uri){
        return data.get(uri);
    }

    public static void load(InputStream is){
        Properties ppt = new Properties();
        try {
            ppt.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //获取配置文件中的一个个类
        Collection<Object> values = ppt.values();
        for (Object cla : values) {
            String classname = (String) cla;
            try {
                //加载文件中的类
                Class c = Class.forName(classname);
                //创建这个类的的对象
                Object o = c.getConstructor().newInstance();
                //获取这个类的所有方法
                Method[] methods = c.getMethods();

                for (Method m : methods) {
                    System.out.println(m.getAnnotatedReturnType());

                    Annotation[] as = m.getAnnotations();
                    if (as != null){
                        for (Annotation a : as) {
                            System.out.println("type"+a.annotationType());
                            if(a instanceof ResponseBody){
                                System.out.println("hahahaha"+((ResponseBody) a).value());
                                //说明此方法是把内容返回给客户端
                                MVCMapping mvcMapping = new MVCMapping(o,m,ResponseType.TEXT);
                                Object t = data.put((((ResponseBody) a).value()),mvcMapping);
                                if(t != null){
                                    throw new RuntimeException("请求地址重复"+((ResponseBody) a).value());
                                }
                            }else if(a instanceof ResponseView){
                                System.out.println("hahahaha"+((ResponseView) a).value());
                                //说明此方法是把页面返回给客户端
                                MVCMapping mvcMapping = new MVCMapping(o,m,ResponseType.VIEW);
                                Object t = data.put((((ResponseView) a).value()),mvcMapping);
                                if(t != null){
                                    throw new RuntimeException("请求地址重复"+((ResponseView) a).value());
                                }

                            }

                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public static class MVCMapping{
        private Object obj;
        private Method method;
        private ResponseType type;

        public MVCMapping() {
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }

        public Method getMethod() {
            return method;
        }

        public void setMethod(Method method) {
            this.method = method;
        }

        public ResponseType getType() {
            return type;
        }

        public void setType(ResponseType type) {
            this.type = type;
        }

        public MVCMapping(Object obj, Method method, ResponseType type) {
            this.obj = obj;
            this.method = method;
            this.type = type;
        }
    }
}
